package com.qdlc.p2p.manage.action.additional;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.UserVipLevelService;
import com.qdlc.p2p.biz.service.WineCouponsService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.constant.WineCouponsStatus;
import com.qdlc.p2p.common.exception.WineCouponsException;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.WineCoupons;
import com.qdlc.p2p.dal.model.WineCouponsModel;

/**
 * 酒券方案管理 Action
 *
 * @author ygw
 * @version 1.1.0
 * @date 2016年5月6日 下午5:40:22
 */
public class ManageWineCouponsAction extends BaseAction<WineCouponsModel> implements ModelDriven<WineCouponsModel> {

    @Resource
    private WineCouponsService wineCouponsService;
    @Resource
    private UserVipLevelService userVipLevelService;

    private WineCouponsModel model = new WineCouponsModel();

    public WineCouponsModel getModel() {
        return model;
    }

    private Map<String, Object> data = new HashMap<String, Object>();

    /**
     * 酒券方案 页面
     *
     * @return
     * @throws Exception
     */
    @Action("/modules/additional/wineCoupons/wineCouponsManager")
    public String wineCouponsManager() throws Exception {
        return "wineCouponsManager";
    }

    /**
     * 酒券方案 列表
     *
     * @throws Exception
     */
    @Action("/modules/additional/wineCoupons/wineCouponsList")
    public void wineCouponsList() throws Exception {
        PageDataList<WineCouponsModel> pageDataList = wineCouponsService.list(model);
        int total = pageDataList.getPage().getTotal();
        data.put("total", total);
        data.put("rows", pageDataList.getList());
        printJson(getStringOfJpaObj(data));
    }

    /**
     * 酒券方案详情 页面
     *
     * @return
     * @throws Exception
     */
    @Action("/modules/additional/wineCoupons/wineCouponsDetailPage")
    public String wineCouponsDetailPage() throws Exception {
        // 获取酒券方案
        WineCoupons wineCoupons = wineCouponsService.getWineCouponsById(model.getId());
        if (null == wineCoupons)
            throw new WineCouponsException(MessageUtil.getMessage("MF101001014"));
        WineCouponsModel wineCouponsModel = WineCouponsModel.instance(wineCoupons);
        request.setAttribute("wineCoupons", wineCouponsModel);
        return "wineCouponsDetailPage";
    }

    /**
     * 酒券方案修改 页面
     *
     * @return
     * @throws Exception
     */
    @Action("/modules/additional/wineCoupons/wineCouponsEditPage")
    public String wineCouponsEditPage() throws Exception {
        // 获取酒券方案
        WineCoupons wineCoupons = wineCouponsService.getWineCouponsById(model.getId());
        if (null == wineCoupons)
            throw new WineCouponsException(MessageUtil.getMessage("MF101001014"));
        WineCouponsModel wineCouponsModel = WineCouponsModel.instance(wineCoupons);
        request.setAttribute("wineCoupons", wineCouponsModel);
        return "wineCouponsEditPage";
    }

    /**
     * 酒券方案修改
     *
     * @throws Exception
     */
    @SuppressWarnings("static-access")
    @Action("/modules/additional/wineCoupons/wineCouponsEdit")
    public void wineCouponsEdit() throws Exception {
        //检测酒券方案常规值
        model.checkAddModel();
        //酒券规则更新，同时更新vip等级配置中的酒券奖励金额
//        userVipLevelService.update(model.getAmount(), model.getId(), model.getPayType());
        //保存酒券方案
        wineCouponsService.update(model);
        printResult(MessageUtil.getMessage("MS101001004"), true);
    }

    /**
     * 酒券方案添加 页面
     *
     * @return
     * @throws Exception
     */
    @Action("/modules/additional/wineCoupons/wineCouponsAddPage")
    public String wineCouponsAddPage() throws Exception {
        return "wineCouponsAddPage";
    }

    /**
     * 酒券方案添加
     *
     * @throws Exception
     */
    @SuppressWarnings("static-access")
    @Action("/modules/additional/wineCoupons/wineCouponsAdd")
    public void wineCouponsAdd() throws Exception {
        //检测酒券方案常规值
        model.checkAddModel();
        //保存酒券方案
        wineCouponsService.save(model);
        printResult(MessageUtil.getMessage("MS101001001"), true);
    }

    /**
     * 酒券方案删除
     *
     * @throws Exception
     */
    @Action("/modules/additional/wineCoupons/wineCouponsDelete")
    public void wineCouponsDelete() throws Exception {
        wineCouponsService.delete(model.getId());
        printResult(MessageUtil.getMessage("MF101001008"), true);
    }

    /**
     * 启用
     *
     * @throws IOException
     */
    @Action(value = "/modules/additional/wineCoupons/wineCouponsEnable")
    public void wineCouponsEnable() throws IOException {
        int result = wineCouponsService.changeStatus(WineCouponsStatus.USE, WineCouponsStatus.NO_USE,model.getId());
        if (result > 0) {
            printWebSuccess();
        } else {
            printWebResult("被禁用的酒券才能被启用!", false);
        }
    }

    /**
     * 禁用
     *
     * @throws IOException
     */
    @Action(value = "/modules/additional/wineCoupons/wineCouponsDisable")
    public void wineCouponsDisable() throws IOException {
        int result = wineCouponsService.changeStatus(WineCouponsStatus.NO_USE, WineCouponsStatus.USE,model.getId());
        if (result > 0) {
            printWebSuccess();
        } else {
            printWebResult("正常的酒券才能被禁用!", false);
        }
    }
}
